package cn.iocoder.yudao.module.jikong.controller.admin.size;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.jikong.controller.admin.size.vo.*;
import cn.iocoder.yudao.module.jikong.dal.dataobject.size.JkSizeDO;
import cn.iocoder.yudao.module.jikong.service.size.JkSizeService;

@Tag(name = "管理后台 - 规格")
@RestController
@RequestMapping("/jikong/jk-size")
@Validated
public class JkSizeController {

    @Resource
    private JkSizeService jkSizeService;

    @PostMapping("/create")
    @Operation(summary = "创建规格")
    @PreAuthorize("@ss.hasPermission('jikong:jk-size:create')")
    public CommonResult<Long> createJkSize(@Valid @RequestBody JkSizeSaveReqVO createReqVO) {
        return success(jkSizeService.createJkSize(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新规格")
    @PreAuthorize("@ss.hasPermission('jikong:jk-size:update')")
    public CommonResult<Boolean> updateJkSize(@Valid @RequestBody JkSizeSaveReqVO updateReqVO) {
        jkSizeService.updateJkSize(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除规格")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('jikong:jk-size:delete')")
    public CommonResult<Boolean> deleteJkSize(@RequestParam("id") Long id) {
        jkSizeService.deleteJkSize(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除规格")
                @PreAuthorize("@ss.hasPermission('jikong:jk-size:delete')")
    public CommonResult<Boolean> deleteJkSizeList(@RequestParam("ids") List<Long> ids) {
        jkSizeService.deleteJkSizeListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得规格")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('jikong:jk-size:query')")
    public CommonResult<JkSizeRespVO> getJkSize(@RequestParam("id") Long id) {
        JkSizeDO jkSize = jkSizeService.getJkSize(id);
        return success(BeanUtils.toBean(jkSize, JkSizeRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得规格分页")
    @PreAuthorize("@ss.hasPermission('jikong:jk-size:query')")
    public CommonResult<PageResult<JkSizeRespVO>> getJkSizePage(@Valid JkSizePageReqVO pageReqVO) {
        PageResult<JkSizeDO> pageResult = jkSizeService.getJkSizePage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, JkSizeRespVO.class));
    }

    @GetMapping("/simple-list")
    @Operation(summary = "获得规格list")
    public CommonResult<PageResult<JkSizeRespVO>> getJkSizeList(@Valid JkSizePageReqVO pageReqVO) {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        PageResult<JkSizeDO> pageResult = jkSizeService.getJkSizePage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, JkSizeRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出规格 Excel")
    @PreAuthorize("@ss.hasPermission('jikong:jk-size:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportJkSizeExcel(@Valid JkSizePageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<JkSizeDO> list = jkSizeService.getJkSizePage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "规格.xls", "数据", JkSizeRespVO.class,
                        BeanUtils.toBean(list, JkSizeRespVO.class));
    }

}
